/**
 * This is a sample controller
 * This file defines methods that are called from the view
 * files. These methods will most commonly handle the changing
 * of panels.
**/

app.controllers.navigation = new Ext.Controller({

    showRestaurantDetails: function (options) {
        record = options.record.data;
        restaurant = restaurantStore.getAt(restaurantStore.find("RESTAURANT_ID", record.RESTAURANT_ID));
        record.restaurant = restaurant;
        var toLatLng = new google.maps.LatLng(record.restaurant.get('LAT2'), record.restaurant.get('LONG2'));
        Ext.getCmp('detailToolbar').setTitle(restaurant.get('RESTAURANT_CATEGORY_NAME'));
        record.distance = 25;
        record.leaveTime = '11:46am';
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(setCurrentPosition);
        }

        else {
            alert("Can't find you");
        }



        //Sets current position
        function setCurrentPosition(position) {
            fromLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            Ext.dispatch({
                controller: app.controllers.Directions,
                action: 'getTimeToLocation',
                fromLatLng: fromLatLng,
                toLatLng: toLatLng,
                record: record
            });
            app.views.restaurantDetail.loadData(record);
            app.views.viewport.setActiveItem(app.views.restaurantDetail, 'slide')
        }

        // call time calculation method here!


    },









    toRankings: function (options) {
        Ext.getCmp('rankingTab').addCls('x-tab-active');
        Ext.getCmp('userTab').removeCls('x-tab-active');
        Ext.getCmp('voteTab').removeCls('x-tab-active');
        app.views.viewport.setActiveItem(
            app.views.rankings, 'fade'
        )
    },
    backToRankings: function (options) {
        app.views.viewport.setActiveItem(
            Ext.getCmp('backButton').prevCard, { type: 'slide', reverse: true }
        )
    },
    showRankings: function (options) {
        Ext.getCmp('userTab').removeCls('x-tab-active');
        Ext.getCmp('rankingTab').addCls('x-tab-active');
        Ext.getCmp('voteTab').removeCls('x-tab-active');
        app.views.viewport.setActiveItem(
            app.views.rankings, 'slide'
        )
    },
    profileListShow: function (options) {
        Ext.getCmp('userTab').addCls('x-tab-active');
        Ext.getCmp('rankingTab').removeCls('x-tab-active');
        Ext.getCmp('voteTab').removeCls('x-tab-active');
        app.views.viewport.setActiveItem(
            app.views.profile, 'fade'
        )
    },
    voteShow: function (options) {
        Ext.getCmp('voteTab').addCls('x-tab-active');
        Ext.getCmp('userTab').removeCls('x-tab-active');
        Ext.getCmp('rankingTab').removeCls('x-tab-active');
        app.views.viewport.setActiveItem(
            app.views.vote, 'fade'
        )
    },
    backToVoteShow: function (options) {
        Ext.getCmp('voteTab').addCls('x-tab-active');
        Ext.getCmp('userTab').removeCls('x-tab-active');
        Ext.getCmp('rankingTab').removeCls('x-tab-active');
        app.views.viewport.setActiveItem(
            app.views.vote, { type: 'slide', reverse: true }
        )
    },
    attendeeShow: function (options) {
        Ext.getCmp('voteTab').addCls('x-tab-active');
        Ext.getCmp('userTab').removeCls('x-tab-active');
        Ext.getCmp('rankingTab').removeCls('x-tab-active');
        app.views.viewport.setActiveItem(
            app.views.attendees, 'slide'
        )
    }
});